import whisper
import os

# 加载模型
model = whisper.load_model("/home/peter/Public/whisper_data/base.pt")

# 音频片段所在路径
audio_path = "/home/peter/Public/audio_data/splited"

# 获取所有音频片段文件列表，并排序
audio_files = sorted([f for f in os.listdir(audio_path) if f.endswith('.mp3')])

# 初始化存储结果的列表
transcripts = []

for idx, file in enumerate(audio_files):
    file_path = os.path.join(audio_path, file)
    
    # 加载和处理音频
    audio = whisper.load_audio(file_path)
    audio = whisper.pad_or_trim(audio)
    mel = whisper.log_mel_spectrogram(audio).to(model.device)
    
    # 识别语言（可选）
    _, probs = model.detect_language(mel)
    detected_language = max(probs, key=probs.get)
    print(f"{idx}: Detected language: {detected_language}")
    
    # 解码音频
    options = whisper.DecodingOptions()
    result = whisper.decode(model, mel, options)
    
    # 打印并存储结果
    print(f"{idx} : {result.text}")
    transcripts.append(result.text)

# 合并所有片段的文本
full_transcription = " ".join(transcripts)

# 打印完整转录结果
print("\n完整转录结果:")
print(full_transcription)
